import { app, BrowserWindow, IpcMainEvent, IpcMainInvokeEvent } from 'electron'
import { createWindow, OptionsType } from './createWindow'
import { createTray } from './tray'

export const config = {
  login: {
    id: 0,
    options: {
      initShow: true,
      hash: '',
      width: 370,
      height: 420,
      minHeight: 420,
      minWidth: 370,
      openDevTools: true,
    },
  },
  config: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1320,
      height: 780,
      minHeight: 780,
      minWidth: 1320,
      hash: '/admin/compute',
    },
  },
  filePage: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1020,
      height: 580,
      minHeight: 580,
      minWidth: 1020,
      hash: '/share/filePage',
    },
  },
  filePages: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1020,
      height: 580,
      minHeight: 580,
      minWidth: 1020,
      hash: '/share/filePages',
    },
  },
  projectDir: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1020,
      height: 580,
      minHeight: 580,
      minWidth: 1020,
      hash: '/project/projectDir',
    },
  },
  projectFile: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1020,
      height: 580,
      minHeight: 580,
      minWidth: 1020,
      hash: '/project/projectFile',
    },
  },
  basiceQuipment: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: true,
      width: 1320,
      height: 780,
      minHeight: 580,
      minWidth: 1020,
      hash: '/compute/basiceQuipment',
    },
  },
  total: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1320,
      height: 780,
      minHeight: 580,
      minWidth: 1020,
      hash: '/compute/total',
    },
  },
  costBalance: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1320,
      height: 780,
      minHeight: 580,
      minWidth: 1020,
      hash: '/cost',
    },
  },
  collectionData: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1028,
      height: 980,
      minWidth: 930,
      hash: '/collection/collectionData',
    },
  },
  ruleList: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 680,
      height: 580,
      minHeight: 580,
      minWidth: 680,
      hash: '/ruleList',
    },
  },
  settingCompute: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      width: 1320,
      height: 780,
      minHeight: 580,
      minWidth: 1020,
      hash: '/compute/settingCompute',
    },
  },
  findSearch: {
    id: 0,
    options: {
      initShow: false,
      openDevTools: false,
      alwaysOnTop: true,
      height: 230,
      width: 680,
      hash: '/search/findSearch',
    },
  },
} as Record<WindowNameType, { id: number; options: OptionsType }>

export const getByNameWindow = (name: WindowNameType, params?: any) => {
  let win = BrowserWindow.fromId(config[name].id)
  if (!win) {
    switch (name) {
      case 'config':
        config[name].options.hash = '/admin/compute'
        break
      case 'filePage':
        config[name].options.hash = '/share/filePage'
        break
      case 'filePages':
        config[name].options.hash = '/share/filePages'
        break
      case 'projectDir':
        config[name].options.hash = '/project/projectDir'
        break
      case 'projectFile':
        config[name].options.hash = '/project/projectFile'
        break
      case 'basiceQuipment':
        config[name].options.hash = '/compute/basiceQuipment'
        break
      case 'total':
        config[name].options.hash = '/compute/total'
        break
      case 'costBalance':
        config[name].options.hash = '/cost'
        break
      case 'collectionData':
        config[name].options.hash = '/collection/collectionData'
        break
      case 'ruleList':
        config[name].options.hash = '/ruleList'
        break
      case 'settingCompute':
        config[name].options.hash = '/compute/settingCompute'
        break
      case 'findSearch':
        config[name].options.hash = '/search/findSearch'
        break
    }
    if (params) {
      const queryString = Object.entries(params)
        .map(([key, value]) => `${key}=${value}`)
        .join('&')
      config[name].options.hash = config[name].options.hash + '?' + queryString
    }
    win = createWindow(config[name].options)
    config[name].id = win.id
  }
  win.show()
  return win
}

export const getWindowByEvent = (event: IpcMainEvent | IpcMainInvokeEvent) => {
  return BrowserWindow.fromWebContents(event.sender)!
}

app.whenReady().then(() => {
  createTray()
  getByNameWindow('login')
})
app.on('window-all-closed', () => {
  if (process.platform == 'darwin') {
    app.quit()
  }
})
